package com.qiedan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiedan.bean.Car;
import com.qiedan.bean.Orders;
import com.qiedan.bean.User;
import com.qiedan.dto.ShopCarDto;
import com.qiedan.mapper.OrdersMapper;
import com.qiedan.mapper.UserMapper;
import com.qiedan.service.CarService;
import com.qiedan.mapper.CarMapper;
import com.qiedan.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class CarServiceImpl extends ServiceImpl<CarMapper, Car>
    implements CarService{

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public R<Object> getShopCarListByUserId(Integer uid) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        List<Car> carList = baseMapper.selectList(queryWrapper);
        ArrayList<ShopCarDto> shopCarDtos = new ArrayList<>();
        log.info("carList: "+carList);
        for(Car car : carList){
            ShopCarDto shopCarDto = new ShopCarDto();
            BeanUtils.copyProperties(car,shopCarDto);

            shopCarDtos.add(shopCarDto);
        }
        R<Object> resp = new R<>();
        resp.add("shopCarItems",shopCarDtos);
        return resp;
    }

    /**
     *  单独下单
     * @param car
     * @return
     */
    @RequestMapping("/pay")
    @Override
    public R<Object> payAlone(@RequestBody Car car) {

        //生成订单
        Orders orders = new Orders();
        BeanUtils.copyProperties(car,orders);
        log.info("orders: "+orders);
        orders.setStatus(1);
        ordersMapper.insert(orders);
        Integer cid = car.getCid();
        //删除购物车选项
        baseMapper.deleteById(cid);
        R<Object> resp = new R<>();
        resp.setMsg("下单成功!");
        return resp;
    }

    /**
     * 加入购物车
     * @param car
     * @return
     */
    @Override
    public R<Object> add(Car car) {

        car.setCreatetime(new Date());
        car.setUpdatetime(new Date());
        car.setNumber(0);
        baseMapper.insert(car);

        R<Object> resp = new R<>();
        resp.setMsg("加入购物车成功 !");
        return resp;
    }


    @Override
    public R<Object> delete(Integer cid) {
        baseMapper.deleteById(cid);
        R<Object> resp = new R<>();
        resp.setMsg("删除成功! ");
        return resp;
    }

    @Override
    public R<Object> clearShopCart(ArrayList<Car> carList) {

        for (Car car : carList){
            Integer number = car.getNumber();
            if(number>0){
                Orders orders = new Orders();
                orders.setIid(car.getIid());
                orders.setUid(car.getUid());
                orders.setCreatetime(new Date());
                Double totalPrices = car.getPrices() * car.getNumber();
                orders.setPrices(totalPrices);
                orders.setNumber(car.getNumber());
                orders.setStatus(1);
                //获取用户地址
                User user = userMapper.selectById(car.getUid());
                String address = user.getAddress();
                orders.setAddress(address);
                ordersMapper.insert(orders);
                //将购物车中相应选项删除
                baseMapper.deleteById(car.getCid());
            }
        }
        R<Object> resp = new R<Object>();
        resp.setMsg("清空购物车成功! ");
        return resp;
    }
}




